package com.tianqi.familymap.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.orhanobut.logger.Logger
import com.tianqi.familymap.R
import com.tianqi.familymap.adapter.RecommendMultiAdapter
import com.tianqi.familymap.base.BaseFragment
import com.tianqi.familymap.model.PageBean
import com.tianqi.familymap.model.RecommendMultipleModel
import com.tianqi.familymap.model.TQBean
import com.tianqi.familymap.presenter.RecommentPresenter
import com.tianqi.familymap.ui.view.IRecommentView
import com.tianqi.familymap.utils.SPUtils


class RecommendFragment : BaseFragment(){
    private lateinit var fgView:View
    private lateinit var mlist:ArrayList<RecommendMultipleModel>
    private val mRecyclerRec by lazy<RecyclerView> {
        fgView.findViewById(R.id.recycler_rec)
    }
    private val mSwipeRefresh by lazy<SwipeRefreshLayout> {
        fgView.findViewById(R.id.swipe_refresh)
    }
    private lateinit var mContext:Context
    private val myAdapter by lazy {
        RecommendMultiAdapter(mlist,mContext)
    }
    private var currentPage = 1

    private val myPresenter by lazy {
        RecommentPresenter(mContext)
    }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        fgView = inflater.inflate(
            R.layout.fragment_recommend, container, false
        )
        initData()
        return fgView
    }

    private fun initData(){
        mContext = activity!!
        mlist = ArrayList()
        val linearLayoutManager = LinearLayoutManager(mContext)
        mRecyclerRec.layoutManager = linearLayoutManager
        val snapHelper = PagerSnapHelper()
        snapHelper.attachToRecyclerView(mRecyclerRec)
        mRecyclerRec.adapter = myAdapter
        myAdapter.setNewInstance(mlist)
        mSwipeRefresh.setColorSchemeResources(R.color.colorBlue)
        mSwipeRefresh.setOnRefreshListener {
            currentPage = 1
            query()
            mSwipeRefresh.isRefreshing = false
        }
        myAdapter.loadMoreModule.setOnLoadMoreListener {
            ++currentPage
            query()
            myAdapter.loadMoreModule.loadMoreEnd(true)
        }
    }

    override fun onResume() {
        super.onResume()
        currentPage = 1
        query()
    }

    private fun query(){
        Logger.i("进入查询")
        val myToken = SPUtils.get(mContext, "token", "").toString()
        myPresenter.onCreate()
        myPresenter.getRecomment(myToken,currentPage)
        myPresenter.attachView(object : IRecommentView {
            override fun showAndroidData(bean: TQBean<PageBean<RecommendMultipleModel>>) {
                if (bean.code == 200){
                    mlist.addAll(bean.data.data)
                    myAdapter.notifyDataSetChanged()
                }
            }

            override fun showErrorMsg(msg: String) {
            }
        })
    }

}